<?php

namespace app\astrteam\model;

use app\common\lib\Aes;
use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class Zlcr3ConsultTeam extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zlcr3_consult_team';

    protected $cacheModel = 'astr_admin.astr_team_info';

    public function SaveData(int $id, array $data)
    {
        try
        {
            $this->where(['id' => $id])->update($data);

            CacheManage::getInstance()->setCacheConfig($this->cacheModel, $id)->rmCache();
            return true;
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return false;
        }
    }

    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string|void
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            $data = $this->whereIn('id', $ids)->column('name,company_name,contact_tel,tax_id,bank_name,bank_account,remark,token,token_expire', 'id');

            $aesModel = new Aes();
            foreach ($data as $teamId => $datum)
            {
                $data[$teamId]['company_name'] = $aesModel->decryptWithOpenssl($datum['company_name'], 'simulation_astrologer');
                $data[$teamId]['tax_id'] = $aesModel->decryptWithOpenssl($datum['tax_id'], 'simulation_astrologer');
                $data[$teamId]['bank_name'] = $aesModel->decryptWithOpenssl($datum['bank_name'], 'simulation_astrologer');
                $data[$teamId]['bank_account'] = $aesModel->decryptWithOpenssl($datum['bank_account'], 'simulation_astrologer');
            }
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }

    public function batchGetInfoByIds($cids, $field = [])
    {
        $resData = parent::batchGetInfoByIds($cids, $field);

        return $this->dataInit($resData);
    }

    /**
     * 数据初始化
     * @param array $data
     * @return array
     */
    public function dataInit(array $data)
    {
        foreach ($data as $index => $itemData)
        {
            $data[$index] = arrayKeyData2int($itemData, ['id', 'token_expire']);
        }

        return $data;
    }

}